<?php

/**
 * Description of scatekeyword
 *
 * @author libaoling
 * @time 2014-12-17
 */
class O2okeywordApp extends BackendApp
{

    private $_o2okeyword_mod;
    private $_o2okeywordlang_mod;
    private $_language_mod;
    public $languages = array();

    function __construct()
    {
        $this->O2okeywordApp();
    }

    function O2okeywordApp()
    {
        parent::__construct();

        $this->_o2okeyword_mod = & m('o2okeyword');
        $this->_o2okeywordlang_mod = & m('o2okeywordlang');
        $this->_language_mod = & m('language');
        $this->languages = $this->_language_mod->get_list();
    }

    /**
     *    管理
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function index()
    {
        $conditions = $this->_get_query_conditions(array(array(
                'field' => 'keyword_search',
                'equal' => 'LIKE',
                'name' => 'search_name',
            ), array(
                'field' => 'recom_id',
                'equal' => '=',
                'type' => 'numeric',
            )
        ));
        
        $page = $this->_get_page(10);    //获取分页信息
        $scatekeyword = $this->_o2okeyword_mod->findAll(array(
            'conditions' => '1=1 ' . $conditions,
            'include' => array('has_o2okeywordlang' => array('index_key' => array('keyword_id', 'lang'))),
            'limit' => $page['limit'], //获取当前页的数据
            'order' => 'sort,keyword_id desc',
            'count' => true             //允许统计
        ));

        $page['item_count'] = $this->_o2okeyword_mod->getCount();   //获取统计的数据
        $this->_format_page($page);

        $this->assign('recom_list', $this->_get_recom_list()); //推荐类型
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
        $this->assign('scatekeyword', $this->_o2okeyword_mod->remake_keywords($scatekeyword));
        $this->assign('languages', $this->languages);

        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->display('scatekeyword.index.html');
    }

    /**
     *    新增
     *
     *    @author    Garbin
     *    @return    void
     */
    function add()
    {
        if (!IS_POST)
        {
            $this->assign('recom_list', $this->_get_recom_list()); //推荐类型
            $this->assign('languages', $this->languages);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('scatekeyword.form.html');
        }
        else
        {

            $data = array();
            $data['keyword_search'] = implode(',', $_POST['keyword']);
            $data['recom_id'] = intval($_POST['recom_id']);
            $data['sort'] = intval($_POST['sort']);
            $data['url'] = trim($_POST['url']);
            $keyword_id = $this->_o2okeyword_mod->add($data);
            if (!$keyword_id)
            {
                $this->show_warning($this->_o2okeyword_mod->get_error());
                return;
            }
            
            foreach ($_POST['keyword'] as $lang => $value)
            {
                $this->_o2okeywordlang_mod->add(array(
                    'keyword' => trim($value),
                    'keyword_id' => $keyword_id,
                    'lang' => $lang,
                    ));
            }

            $logo = $this->_upload_logo($keyword_id);
            $logo && $this->_o2okeyword_mod->edit($keyword_id, array('logo' => $logo)); //将logo地址记下

            $this->show_message('添加成功', 'back_list', 'index.php?app=o2okeyword', '继续添加', 'index.php?app=o2okeyword&amp;act=add'
            );
        }
    }

    /**
     *    编辑
     *
     *    @author    Garbin
     *    @return    void
     */
    function edit()
    {
        $keyword_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$keyword_id)
        {
            $this->show_warning('没有相关的数据');

            return;
        }
        if (!IS_POST)
        {
            $find_data = $this->_o2okeyword_mod->get_by_id($keyword_id);
            if (empty($find_data))
            {
                $this->show_warning('没有相关的数据');

                return;
            }
            
            $scatekeyword = current($find_data);
            $this->assign('scatekeyword', $scatekeyword);
            $this->assign('recom_list', $this->_get_recom_list()); //推荐类型
            $this->assign('languages', $this->languages);
            
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('scatekeyword.form.html');
        }
        else
        {
            $data = array();
            $data['keyword_search'] = implode('|', $_POST['keyword']);
            $data['recom_id'] = intval($_POST['recom_id']);
            $data['sort'] = intval($_POST['sort']);
            $data['url'] = trim($_POST['url']);

            $logo = $this->_upload_logo($keyword_id);
            $logo && $data['logo'] = $logo;

            $this->_o2okeyword_mod->edit($keyword_id, $data);
            if ($this->_o2okeyword_mod->has_error())    //有错误
            {
                $this->show_warning($this->_o2okeyword_mod->get_error());

                return;
            }
            
            foreach ($_POST['keyword'] as $lang => $value)
            {
                $this->_o2okeywordlang_mod->add(array(
                    'keyword' => trim($value),
                    'keyword_id' => $keyword_id,
                    'lang' => $lang,
                    ), true);
            }

            $this->show_message('编辑成功', 'back_list', 'index.php?app=o2okeyword', '再次编辑', 'index.php?app=o2okeyword&amp;act=edit&amp;id=' . $keyword_id);
        }
    }

    //异步修改数据
    function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();

        $fileds = $lang_fileds = $this->_get_lang_fileds('keyword');
        $fileds[] = 'sort';
        if (in_array($column, $fileds))
        {
            if (in_array($column, $lang_fileds))
            {
                $lang = substr($column, -1, 1);
                $this->_o2okeywordlang_mod->add(array(
                    'keyword' => $value,
                    'keyword_id' => $id,
                    'lang' => $lang,
                    ), true);
            }
            else
            {
                $data[$column] = $value;
                $this->_o2okeyword_mod->edit($id, $data);
                if (!$this->_o2okeyword_mod->has_error())
                {
                    echo ecm_json_encode(true);
                }
            }
        }
        else
        {
            return;
        }
        return;
    }

    function drop()
    {
        $keyword_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
        if (!$keyword_ids)
        {
            $this->show_warning('请选择要删除的数据');

            return;
        }
        $keyword_ids = explode(',', $keyword_ids); //获取一个类似array(1, 2, 3)的数组
        if (!$this->_o2okeyword_mod->drop($keyword_ids))    //删除
        {
            $this->show_warning($this->_o2okeyword_mod->get_error());

            return;
        }
        
        $this->_o2okeywordlang_mod->drop($keyword_ids);

        $this->show_message('删除成功');
    }

    /* 获取推荐类型 */

    function _get_recom_list()
    {
        $file = ROOT_PATH . "/wl_admin/includes/config/o2o.keyword.php";
        $arr = file_exists($file) ? require $file : array();

        return $arr;
    }

    function _upload_logo($keyword_id)
    {
        $file = $_FILES['logo'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
        {
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['logo']); //上传logo

        if (!$uploader->file_info())
        {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=scategory&amp;act=edit&amp;id=' . $keyword_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        /* 上传 */
        if ($file_path = $uploader->save('data/files/o2o/scate/keyword', $keyword_id))   //保存到指定目录，并以指定文件名$adv_id存储
        {
            return $file_path;
        }
        else
        {
            return false;
        }
    }

}
